Skip to content

[Console] Document #[Argument] and #[Option] attributes for invokable commands#21652

Merged
javiereguiluz merged 1 commit intosymfony:7.3from
yceruto:console_input_attributes
Jan 14, 2026
Merged

[Console] Document #[Argument] and #[Option] attributes for invokable commands#21652
javiereguiluz merged 1 commit intosymfony:7.3from
yceruto:console_input_attributes

Conversation

@yceruto
Copy link
Copy Markdown
Member

@yceruto yceruto commented Dec 19, 2025

Part of #20553.

I kept both approaches for now so that later we can extract the classic approach into a new section as part of #21168.

{
public function __invoke(
#[Option(shortcut: 'f')] bool $foo = false,
#[Option(shortcut: 'b')] string $bar = '',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value of the bar option (and its b shortcut respectively) is required.

So default to empty string must be removed ? (en reorder args)

This sentence if not true with options attributes write ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's true, and it refers to the moment when the option is used; at that point, we're forced to set a value for that option.

in other words, having a default value (which is required for options) means it's the value used when the option is not passed on the command line. When the option is passed, it must include a value, e.i. --opt=1, passing just --opt is forbidden under this definition

@yceruto yceruto force-pushed the console_input_attributes branch from 9a523f3 to 0a2c166 Compare January 5, 2026 11:21
Copy link
Copy Markdown
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great contribution! Thanks Yonel.

I only have a minor question after reviewing the contents.


``name``
The name of the argument (by default, the parameter name converted to ``kebab-case``).
For example: ``#[Argument(name: 'user-name')]``.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this name used? Because user-name would be no valid PHP variable name, so I'm not sure where this value is used in practice. Thanks.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These names show up in the help output, but also it's still possible to get an argument via $input->getArgument('user-name')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use this name when we want it to differ from the property/parameter name.


``name``
The name of the option (by default, the parameter name converted to ``kebab-case``).
For example: ``#[Option(name: 'max-retries')]``.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here as in the arguments section.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is more visible, since the user will use this name to pass the option: --max-reties=10

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiereguiluz javiereguiluz merged commit 2158fdf into symfony:7.3 Jan 14, 2026
2 of 3 checks passed
@javiereguiluz
Copy link
Copy Markdown
Member

This is now merged! Thanks Yonel for such a nice contribution.

Also, while merging I expanded some explanations a bit based on your feedback to my comments. See 3f81441

Thanks!

@yceruto yceruto deleted the console_input_attributes branch January 14, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants